package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Cart;
import com.cskaoyan.bean.User1;
import com.cskaoyan.bean.cart.*;
import com.cskaoyan.service.CartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("wx/cart")
public class WxCartController {

    @Autowired
    CartService cartService;

    @RequestMapping("index")
    public BaseRespVo index(){
        CartIndexBean cartIndexBean = cartService.selectCartIndex();
        return BaseRespVo.ok(cartIndexBean);
    }

    @RequestMapping("add")
    public BaseRespVo add(@RequestBody CartListBean cartListBean){
        PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
        if (principals == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        Integer data = cartService.addCart(cartListBean);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody FastAddBean fastAddBean){
        Integer isHaved = cartService.selectCartByFastAddBean(fastAddBean);
        return BaseRespVo.ok(isHaved);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody CartUpdateBean cartUpdateBean){
       cartService.updateCart(cartUpdateBean);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CartDelectBean cartDelectBean){
       cartService.delectCart(cartDelectBean);     //先删除再查询（调用index方法）
       CartIndexBean cartIndexBean = cartService.selectCartIndex();
        return BaseRespVo.ok(cartIndexBean);
    }
    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody CheckedBean checkedBean){
       cartService.updateChecked(checkedBean);
        CartIndexBean cartIndexBean = cartService.selectCartIndex();
        return BaseRespVo.ok(cartIndexBean);
    }

    @RequestMapping("goodscount")
    public BaseRespVo goodscount(){
       Integer data = cartService.selectGoodsCount();
        return BaseRespVo.ok(data);
    }


//    @RequestMapping("checkout")
//   public BaseRespVo checkout(CheckoutBean checkoutBean){
//        CartCheckoutBean cartCheckoutBean = cartService.checkoutCart(checkoutBean);
//        return BaseRespVo.ok(cartCheckoutBean);
//    }

    @RequestMapping("checkout")
    public BaseRespVo checkOutCart(Integer cartId,Integer addressId,Integer couponId,Integer grouponRulesId){
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipals().getPrimaryPrincipal();
        Integer userId = user.getId();
        CheckOutVo checkOutVo=cartService.selectCheckOutVo(userId,cartId,addressId,couponId,grouponRulesId);
        return BaseRespVo.ok(checkOutVo);
    }



}
